import wx
#import wx.aui

try:
    from agw import aui
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.aui as aui

import cStringIO

import Plugin



#----------------------------------------------------------------------
def GetMondrianData():
	return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x06\x00\
\x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00qID\
ATX\x85\xed\xd6;\n\x800\x10E\xd1{\xc5\x8d\xb9r\x97\x16\x0b\xad$\x8a\x82:\x16\
o\xda\x84pB2\x1f\x81Fa\x8c\x9c\x08\x04Z{\xcf\xa72\xbcv\xfa\xc5\x08 \x80r\x80\
\xfc\xa2\x0e\x1c\xe4\xba\xfaX\x1d\xd0\xde]S\x07\x02\xd8>\xe1wa-`\x9fQ\xe9\
\x86\x01\x04\x10\x00\\(Dk\x1b-\x04\xdc\x1d\x07\x14\x98;\x0bS\x7f\x7f\xf9\x13\
\x04\x10@\xf9X\xbe\x00\xc9 \x14K\xc1<={\x00\x00\x00\x00IEND\xaeB`\x82' 


def GetMondrianBitmap():
	return wx.BitmapFromImage(GetMondrianImage())


def GetMondrianImage():
	stream = cStringIO.StringIO(GetMondrianData())
	return wx.ImageFromStream(stream)


def GetMondrianIcon():
	icon = wx.EmptyIcon()
	icon.CopyFromBitmap(GetMondrianBitmap())
	return icon


class AUIFrame(wx.Frame):
	
	def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
				 size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE |
											wx.SUNKEN_BORDER):

		wx.Frame.__init__(self, parent, id, title, pos, size, style)

		# tell FrameManager to manage this frame		
		self.__auiManager = aui.AuiManager()
		self.__auiManager.SetManagedWindow(self)

		#cm
		self.__auiManager.GetArtProvider().SetMetric(aui.AUI_DOCKART_PANE_BORDER_SIZE,1)
		self.__auiManager.GetArtProvider().SetMetric(aui.AUI_DOCKART_SASH_SIZE,1)
		self.__auiManager.GetArtProvider().SetMetric(aui.AUI_DOCKART_CAPTION_SIZE,10)


		self.SetIcon(GetMondrianIcon())


		ID_About = wx.NewId()
		ID_PluginsHelp = wx.NewId()
		# create menu
		mb = wx.MenuBar()

		file_menu = wx.Menu()
		file_menu.Append(wx.ID_EXIT, "Exit")

		help_menu = wx.Menu()
		help_menu.Append(ID_PluginsHelp, "&PluginsHelp...")
		help_menu.Append(ID_About, "&About...")

		mb.Append(file_menu, "&File")
		mb.Append(help_menu, "&Help")

		self.SetMenuBar(mb)

		self.statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
		self.statusbar.SetStatusWidths([-2, -3])
		self.statusbar.SetStatusText("Ready", 0)
		self.statusbar.SetStatusText("Welcome To Ogre Editor!", 1)

		# min size for the frame itself isn't completely done.
		# see the end up FrameManager::Update() for the test
		# code. For now, just hard code a frame minimum size
		self.SetMinSize(wx.Size(800, 600))


		# add a bunch of panes
		# self.__auiManager.AddPane(self.CreateTreeCtrl(), aui.AuiPaneInfo().
						  # Name("test0").Caption("Tree Pane").
						  # Left().Layer(1).Position(1).CloseButton(False).MaximizeButton(True).MinimizeButton(True))

		# self.__auiManager.AddPane(self.CreateTextCtrl(), aui.AuiPaneInfo().
						  # Name("test1").Caption("Text Pane").
						  # Bottom().Layer(1).Position(1).CloseButton(False).MaximizeButton(True).MinimizeButton(True))

		# # create some center panes
		# self.__auiManager.AddPane(self.CreateTextCtrl(), aui.AuiPaneInfo().Name("text_content").
						  # CenterPane())


		# add the toolbars to the manager
		
		# make some default perspectives

		self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
		self.Bind(wx.EVT_SIZE, self.OnSize)
		self.Bind(wx.EVT_CLOSE, self.OnClose)

		# Show How To Use The Closing Panes Event
		self.Bind(aui.EVT_AUI_PANE_CLOSE, self.OnPaneClose)

		self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
		self.Bind(wx.EVT_MENU, self.OnPluginsHelp, id=ID_PluginsHelp)
		self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_About)


		self.plugins = Plugin.Plugins()

	def OnPaneClose(self, event):

		# caption = event.GetPane().caption

		# if caption in ["Tree Pane", "Dock Manager Settings", "Fixed Pane"]:
			# msg = "Are You Sure You Want To Close This Pane?"
			# dlg = wx.MessageDialog(self, msg, "AUI Question",
								   # wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)

			# if dlg.ShowModal() in [wx.ID_NO, wx.ID_CANCEL]:
				# event.Veto()
			# dlg.Destroy()
		pass

	def OnClose(self, event):
		
		dlg = wx.MessageDialog(self,
								'Are you sure to close OgreEditor!',
		                       	'A Message Box',
		                       	wx.YES_NO | wx.ICON_INFORMATION | wx.NO_DEFAULT
		                       	#wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
		                       	)
		is_yes = dlg.ShowModal()
		dlg.Destroy()
		if is_yes == wx.ID_YES :
			
			self.plugins.shutdowm()

			self.__auiManager.UnInit()
			del self.__auiManager
			self.Destroy()

	def OnExit(self, event):
		self.Close()

	def OnPluginsHelp(self,e):
		import  wx.lib.dialogs

		doc = self.plugins.help()

		dlg = wx.lib.dialogs.ScrolledMessageDialog(self, doc, "Plugins Help")
		dlg.ShowModal()
		dlg.Destroy()
		pass

	def OnAbout(self, event):
		info = wx.AboutDialogInfo()
		info.Name = "Ogre Edit"
		info.Version = "0.0.0"
		info.Copyright = "copy right ... "
		info.Description = 'Description ...'
		info.WebSite = ("http://...", "Ogre Edit home page")
		info.Developers = [ "LanYuan",
                            "...",
                            "..." ]

		info.License = 'License ...'

		# Then we call wx.AboutBox giving it that info object
		wx.AboutBox(info)

#		msg = "wx.aui Demo\n" + \
#			  "An advanced window management library for wxWidgets\n" + \
#			  "(c) Copyright 2005-2006, Kirix Corporation"
#		dlg = wx.MessageDialog(self, msg, "About wx.aui Demo",
#							   wx.OK | wx.ICON_INFORMATION)
#		dlg.ShowModal()
#		dlg.Destroy()		

	def DoUpdate(self):
		self.__auiManager.Update()

	def OnEraseBackground(self, event):
		event.Skip()

	def OnSize(self, event):
		event.Skip()

	def getAuiManager(self):
		return self.__auiManager

	def initPlugin(self,ModuleName,path = 'plugins.'):
		module = __import__(path + ModuleName)
		#help(module)
		exec 'self.plugins.' + ModuleName + ' = ' + 'module.' + ModuleName + '.IInit(self)'
		pass


#----------------------------------------------------------------------

if __name__ == '__main__':
	app = wx.PySimpleApp()
	frame = AUIFrame(None, wx.ID_ANY, "wx.aui wxPython Demo", size=(750, 590))
	frame.Show(True)
	app.MainLoop()


